package com.kitty.game.equip.handler;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.config.Equip;
import com.kitty.game.equip.EquipDataPool;
import lombok.extern.slf4j.Slf4j;
import org.nutz.lang.Strings;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import com.kitty.game.equip.handler.UpgradeHandler;

import static com.kitty.game.utils.Const.*;
import static com.kitty.game.utils.NotifyModules.NOTIFY_UPGRADE_JEWELRY_OK;

@Component("JewelryComposeHandler")
@Slf4j
public class JewelryComposeHandler extends UpgradeHandler {
    private static final String ERROR_RESOURCES = "请放入正确的装备或材料。";
    private static final String STONE = "超级女娲石";
    private static final String SUCCESS ="恭喜你，成功合成了{0}件#R{1}#n。";

    @Override
    public String doUpgrade(Role role, Object... params) {
        Equip equip = (Equip) params[0];
        boolean isUseLimit = (boolean) params[1];
        short newPos = (short) params[2];
        boolean onekeyCompose = (boolean) params[3];

        int needCash =needCash(equip.getReq_level(),0);
        int limitNum =0;
        int delNum =3;
        int composeNum =1;
        /**一键合成 只有低于80级的能一键合成*/
        if(onekeyCompose){
            int equipNum = equipService.getCount(equip.getName(), isUseLimit, role);
            /**能合成的数量等于总数量除/3*/
            composeNum=equipNum/3;
            /**要删除的物品等于合成数量乘3*/
            delNum=composeNum*3;
            /**需要扣的钱等于一个需要的钱乘合成的数量*/
            needCash*=composeNum;
        }
        log.error("合成首饰消耗金钱={}",needCash);
        /**扣钱*/
        roleService.subtractMoney(role,needCash);
        /**低于80级只扣除3个低一级的首饰*/
        if(equip.getReq_level()<80){
            limitNum+=equipService.delRoloEquip(equip.getName(),delNum,isUseLimit,role);
        }else if(equip.getReq_level()==80){
            /**高于80级的不能该一键 所以扣除固定的数量*/
            limitNum+=equipService.delRoloEquip(equip.getName(),1,isUseLimit,role);
            limitNum+=equipService.delRoloEquip(STONE,2,isUseLimit,role);
        }

        RoleEquip  roleEquip = equipService.getJewelry(role, equip, "金色", composeNum, limitNum, null, newPos, false,false);
        short i = roleEquip.queryLevel();
        if(i==80){
            roleService.addAchieveScore(role,"302023");
        }else if(i==90){
            roleService.addAchieveScore(role,"302010");
        }else if(i==100){
            roleService.addAchieveScore(role,"302024");
        }else if(i==110){
            roleService.addAchieveScore(role,"302006");
        }else if(i==120){
            roleService.addAchieveScore(role,"302013");
        }else if(i==130){
            roleService.addAchieveScore(role,"302002");
        }

        refreshRoleEquip(role,roleEquip);
        pushOperateResult(role,UPGRADE_SUCCESS);
        return MessageFormat.format(SUCCESS,roleEquip.queryNum(),roleEquip.getName());
    }

    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        String composeName = param.split("_")[0];
        boolean isUseLimit = param.split("_")[1].equals("1");
        boolean onekeyCompose =param.split("_")[2].equals("1");
        Equip equip = EquipDataPool.getByName(composeName);
        if (equip == null) {
            log.error("找不到装备配置=[{}]", param);
            return ERROR_RESOURCES;
        }
        /**合成80级及以上首饰不能一键合成*/
        if (equip.getReq_level() >= 80) {
            onekeyCompose = false;
        }
        if (!isRightPosition(equip.getPosition())) {
            return ERROR_RESOURCES;
        }
        short newPos = getVacancy(role);
        if (newPos <= 0) {
            return ROLE_VACANCY_NOT_ENOUGH;
        }
        String content = checkResource(role, equip,isUseLimit,onekeyCompose);
        if (!Strings.isEmpty(content)) {
            return content;
        }

        return doUpgrade(role, equip, isUseLimit, newPos,onekeyCompose);
    }

    @Override
    public String checkResource(Object... objects) {
        Role role = (Role) objects[0];
        Equip equip = (Equip) objects[1];
        String needName = equip.getName();
        short level = equip.getReq_level();
        boolean isUseLimit = (boolean) objects[2];
        boolean onekeyCompose =(boolean) objects[3];

        int needCash = needCash(level, 0);

        int equipNum = equipService.getCount(needName, isUseLimit, role);
        int stoneNum = equipService.getCount(STONE, isUseLimit, role);
        if (level < 80) {
            if (equipNum < 3) {
                return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, needName);
            }
            /**一键合成*/
            if(onekeyCompose){
                int composeNum =equipNum/3;
                needCash*=composeNum;
            }
            if (role.getMoney() < needCash) {
                return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, MONEY_NAME);
            }

        } else if (level == 80) {
            if (role.getMoney() < needCash) {
                return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, MONEY_NAME);
            }

            if (equipNum < 1) {
                return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, needName);
            }
            if (stoneNum < 2) {
                return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, STONE);
            }
        } else {
            return ERROR_OPTYPE;
        }
        return null;
    }

    @Override
    public int needCash(short level, int pos) {
        if (level < 50) {
            return 0;
        } else if (level < 80) {
            return level * 500;
        } else  {
            return level * 6000;
        }
    }

    @Override
    public String getUpdateTypeStr() {
        return "jewelry_compose";
    }

    @Override
    int getNotifyType() {
        return NOTIFY_UPGRADE_JEWELRY_OK;
    }

    /**
     * 判断打造的物品装备的部位是否符合条件
     */
    @Override
    boolean isRightPosition(byte equipPos) {
        return equipPos == NECKLACE || equipPos == BALDRIC || equipPos == LEFT_WRIST;
    }
}
